﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using FFS.Model;
using FFS.IDAL;
using FFS.DBUtility;

namespace FFS.SQLDAL
{
    public class SystemLogDAO:ISystemLogDAO
    {
        private const string LOGIN_USER_NAME = "@LoginUserName";
        private const string ACTION_TYPE = "@ActionType";
        private const string ACTION_LOG = "@ActionLog";
        private const string INPUT_USER = "@input_user";
        private const string SELECT_SYSTEM_LOG = @"SELECT  [LoginUserName]
                                                          ,[ActionType]
                                                          ,[ActionLog]
                                                          ,[input_date]
                                                          ,[input_user]
                                                      FROM [dbo].[LoginUserActionLog]  
                                                      
                                                      WHERE  ((isnull(@ActionType,N'') <> N'' and [ActionType] = @ActionType) or (isnull(@ActionType,N'') = N''))                                                   

                                                  ";

        private List<SystemLogInfo> GetQueryList(CommandType cmdType,string sqlText,params SqlParameter[] param)
        {
            List<SystemLogInfo> systemLogList = new List<SystemLogInfo>();
            using (SqlDataReader reader =  DBHelper.ExecuteReader(cmdType,sqlText,param))
            {
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        SystemLogInfo sysLog = new SystemLogInfo();
                        sysLog.LoginUserName = reader.GetString(0);
                        sysLog.ActionType = reader.GetString(1);
                        sysLog.ActionLog = reader.GetString(2);
                        sysLog.InputDate = reader.GetDateTime(3);
                        sysLog.InputUser = reader.GetString(4);
                        systemLogList.Add(sysLog);
                    }
                }
            }

            return systemLogList;
        }
        
        
        #region ISystemLogDAO 成员

        public int CreateLoginUserActionLog(string loginUserName, string actionType, string actionLog, string input_user)
        {
            string sqlText = @" INSERT INTO [dbo].[LoginUserActionLog]
                                               ([LoginUserName]
                                               ,[ActionType]
                                               ,[ActionLog]
                                               ,[is_deleted]
                                               ,[input_date]
                                               ,[input_user]) 
                                values(@LoginUserName,@ActionType,@ActionLog,0,getdate(),@input_user)
                             ";
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter(LOGIN_USER_NAME, SqlDbType.NVarChar);
            param[0].Value = loginUserName;
            param[1] = new SqlParameter(ACTION_TYPE, actionType);
            param[2] = new SqlParameter(ACTION_LOG, actionLog);
            param[3] = new SqlParameter(INPUT_USER, input_user);

            return DBHelper.ExecuteNonQuery(CommandType.Text, sqlText, param);
        }


        public List<SystemLogInfo> GetSystemLogByActionType(string actionType,string searchCondition)
        {
            string sqlText = SELECT_SYSTEM_LOG + searchCondition + " order by input_date desc ";
            SqlParameter param = new SqlParameter(ACTION_TYPE,actionType);

            return this.GetQueryList(CommandType.Text, sqlText, param);
        }

        #endregion
    }
}
